package com.jason.lifecycleview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.jason.lifecycleview.CustomCheckBoxChangeListener;
import com.jason.lifecycleview.R;

/**
 * @Author: Jason
 * @Date: 2016/11/8
 * @Time: 16:11.
 */

public class CustomCheckBox extends FrameLayout {

    private static final String TAG = CustomCheckBox.class.getSimpleName();

    private ImageView ivCheckOn, ivCheckOff;

    private CustomCheckBoxChangeListener mCustomCheckBoxChangeListener;

    private boolean isCheck;


    public CustomCheckBox(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        Log.e(TAG, "  CustomCheckBox(Context context, AttributeSet attrs)  ");

    }

    public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.e(TAG, "  CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr)  ");
        LayoutInflater.from(context).inflate(R.layout.checkbox, this);

        ivCheckOff = (ImageView) findViewById(R.id.view_custom_check_box_off);
        ivCheckOn = (ImageView) findViewById(R.id.view_custom_check_box_on);

        // 设置点事件
        ivCheckOn.setOnClickListener(new ClickListener());
        ivCheckOff.setOnClickListener(new ClickListener());

        // 读取xml中设置的资源属性ID
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomCheckBox);
        int imageOnResId = array.getResourceId(R.styleable.CustomCheckBox_CustomCheckBox_imageOn, -1);
        int imageOffResId = array.getResourceId(R.styleable.CustomCheckBox_CustomCheckBox_imageOff, -1);

        Log.e(TAG, "---imageOnResId----" + imageOnResId);
        Log.e(TAG, "---imageOffResId----" + imageOffResId);

        ivCheckOn.setImageResource(imageOnResId);
        ivCheckOff.setImageResource(imageOffResId);

        array.recycle();

        //设置默认没选中
        setCheckOff();


    }

    // 设置监听

    public void setCustomCheckBoxChangeListener(CustomCheckBoxChangeListener listener) {
        this.mCustomCheckBoxChangeListener = listener;
    }

    // 设置为关闭状态
    public void setCheckOff() {
        isCheck = false;
        ivCheckOn.setVisibility(View.GONE);
        ivCheckOff.setVisibility(View.VISIBLE);
    }

    // 设置为开启状态
    public void setCheckOn() {
        isCheck = true;
        ivCheckOff.setVisibility(View.GONE);
        ivCheckOn.setVisibility(View.VISIBLE);
    }


    // 设置显示图片
    public void setOnImage(int resID) {
        ivCheckOn.setImageResource(resID);
    }

    // 设置关闭图片
    public void setOffImage(int resID) {
        ivCheckOff.setImageResource(resID);
    }


    public boolean isCheck() {
        return isCheck;
    }


    private class ClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.view_custom_check_box_on:
                    setCheckOff();
                    Log.e(TAG, "   view_custom_check_box_on   点击   ");
                    mCustomCheckBoxChangeListener.customCheckBoxOff();
                    break;
                case R.id.view_custom_check_box_off:
                    setCheckOn();
                    Log.e(TAG, "   view_custom_check_box_off   点击   ");
                    mCustomCheckBoxChangeListener.customCheckBoxOn();
                    break;
            }
        }
    }
}
